#include <stdio.h> 
#include <string.h> 
#include "qwerty_py.h"



struct PY_index 
{ 
		MEM_ATTR char *PY; 
		MEM_ATTR char *PY_mb; 
}; 


/*"拼音输入法查询码表,二级字母索引表(index)"*/ 
MEM_ATTR struct PY_index		PY_index_a[]={{"",PY_mb_a}, 
																{"i",PY_mb_ai}, 
																{"n",PY_mb_an}, 
																{"ng",PY_mb_ang}, 
																{"o",PY_mb_ao}};

MEM_ATTR struct PY_index		PY_index_b[]={{"a",PY_mb_ba}, 
																{"ai",PY_mb_bai}, 
																{"an",PY_mb_ban}, 
																{"ang",PY_mb_bang}, 
																{"ao",PY_mb_bao}, 
																{"ei",PY_mb_bei}, 
																{"en",PY_mb_ben}, 
																{"eng",PY_mb_beng}, 
																{"i",PY_mb_bi}, 
																{"ian",PY_mb_bian}, 
																{"iao",PY_mb_biao}, 
																{"ie",PY_mb_bie}, 
																{"in",PY_mb_bin}, 
																{"ing",PY_mb_bing}, 
																{"o",PY_mb_bo}, 
																{"u",PY_mb_bu}};

MEM_ATTR struct PY_index		PY_index_c[]={{"a",PY_mb_ca}, 
																{"ai",PY_mb_cai}, 
																{"an",PY_mb_can}, 
																{"ang",PY_mb_cang}, 
																{"ao",PY_mb_cao}, 
																{"e",PY_mb_ce}, 
																{"en",PY_mb_cen},
																{"eng",PY_mb_ceng}, 
																{"ha",PY_mb_cha}, 
																{"hai",PY_mb_chai}, 
																{"han",PY_mb_chan}, 
																{"hang",PY_mb_chang}, 
																{"hao",PY_mb_chao}, 
																{"he",PY_mb_che}, 
																{"hen",PY_mb_chen}, 
																{"heng",PY_mb_cheng}, 
																{"hi",PY_mb_chi}, 
																{"hong",PY_mb_chong}, 
																{"hou",PY_mb_chou}, 
																{"hu",PY_mb_chu}, 
																{"huai",PY_mb_chuai}, 
																{"huan",PY_mb_chuan}, 
																{"huang",PY_mb_chuang}, 
																{"hui",PY_mb_chui}, 
																{"hun",PY_mb_chun}, 
																{"huo",PY_mb_chuo}, 
																{"i",PY_mb_ci}, 
																{"ong",PY_mb_cong}, 
																{"ou",PY_mb_cou}, 
																{"u",PY_mb_cu}, 
																{"uan",PY_mb_cuan}, 
																{"ui",PY_mb_cui}, 
																{"un",PY_mb_cun}, 
																{"uo",PY_mb_cuo}};

MEM_ATTR struct PY_index		PY_index_d[]={{"a",PY_mb_da}, //以前为"a   "则无法显示da
																{"ai",PY_mb_dai}, 
																{"an",PY_mb_dan}, 
																{"ang",PY_mb_dang}, 
																{"ao",PY_mb_dao}, 
																{"e",PY_mb_de}, 
																{"ei",PY_mb_dei},
																{"eng",PY_mb_deng}, 
																{"i",PY_mb_di}, 
																{"ia",PY_mb_dia},
																{"ian",PY_mb_dian}, 
																{"iao",PY_mb_diao}, 
																{"ie",PY_mb_die}, 
																{"ing",PY_mb_ding}, 
																{"iu",PY_mb_diu}, 
																{"ong",PY_mb_dong}, 
																{"ou",PY_mb_dou}, 
																{"u",PY_mb_du}, 
																{"uan",PY_mb_duan}, 
																{"ui",PY_mb_dui}, 
																{"un",PY_mb_dun}, 
																{"uo",PY_mb_duo}};

MEM_ATTR struct PY_index		PY_index_e[]={{"",PY_mb_e}, 
																{"i",PY_mb_ei},
																{"n",PY_mb_en}, 
																{"r",PY_mb_er}};

MEM_ATTR struct PY_index		PY_index_f[]={{"a",PY_mb_fa}, 
																{"an",PY_mb_fan}, 
																{"ang",PY_mb_fang}, 
																{"ei",PY_mb_fei}, 
																{"en",PY_mb_fen}, 
																{"eng",PY_mb_feng}, 
																{"o",PY_mb_fo}, 
																{"ou",PY_mb_fou}, 
																{"u",PY_mb_fu}};

MEM_ATTR struct PY_index		PY_index_g[]={{"a",PY_mb_ga}, 
																{"ai",PY_mb_gai}, 
																{"an",PY_mb_gan}, 
																{"ang",PY_mb_gang}, 
																{"ao",PY_mb_gao}, 
																{"e",PY_mb_ge}, 
																{"ei",PY_mb_gei}, 
																{"en",PY_mb_gan}, 
																{"eng",PY_mb_geng}, 
																{"ong",PY_mb_gong}, 
																{"ou",PY_mb_gou}, 
																{"u",PY_mb_gu}, 
																{"ua",PY_mb_gua}, 
																{"uai",PY_mb_guai}, 
																{"uan",PY_mb_guan}, 
																{"uang",PY_mb_guang}, 
																{"ui",PY_mb_gui}, 
																{"un",PY_mb_gun}, 
																{"uo",PY_mb_guo}};

MEM_ATTR struct PY_index		PY_index_h[]={{"a",PY_mb_ha}, 
																{"ai",PY_mb_hai}, 
																{"an",PY_mb_han}, 
																{"ang",PY_mb_hang}, 
																{"ao",PY_mb_hao}, 
																{"e",PY_mb_he}, 
																{"ei",PY_mb_hei}, 
																{"en",PY_mb_hen}, 
																{"eng",PY_mb_heng}, 
																{"ong",PY_mb_hong}, 
																{"ou",PY_mb_hou}, 
																{"u",PY_mb_hu}, 
																{"ua",PY_mb_hua}, 
																{"uai",PY_mb_huai}, 
																{"uan",PY_mb_huan}, 
																{"uang",PY_mb_huang}, 
																{"ui",PY_mb_hui}, 
																{"un",PY_mb_hun}, 
																{"uo",PY_mb_huo}};

MEM_ATTR struct PY_index  	PY_index_i[]={{"",PY_mb_space}};

MEM_ATTR struct PY_index  	PY_index_j[]={{"i",PY_mb_ji}, 
																{"ia",PY_mb_jia}, 
																{"ian",PY_mb_jian}, 
																{"iang",PY_mb_jiang}, 
																{"iao",PY_mb_jiao}, 
																{"ie",PY_mb_jie}, 
																{"in",PY_mb_jin}, 
																{"ing",PY_mb_jing}, 
																{"iong",PY_mb_jiong}, 
																{"iu",PY_mb_jiu}, 
																{"u",PY_mb_ju}, 
																{"uan",PY_mb_juan}, 
																{"ue",PY_mb_jue}, 
																{"un",PY_mb_jun}};

MEM_ATTR struct PY_index		PY_index_k[]={{"a",PY_mb_ka}, 
																{"ai",PY_mb_kai}, 
																{"an",PY_mb_kan}, 
																{"ang",PY_mb_kang}, 
																{"ao",PY_mb_kao}, 
																{"e",PY_mb_ke}, 
																{"en",PY_mb_ken}, 
																{"eng",PY_mb_keng}, 
																{"ong",PY_mb_kong}, 
																{"ou",PY_mb_kou}, 
																{"u",PY_mb_ku}, 
																{"ua",PY_mb_kua}, 
																{"uai",PY_mb_kuai}, 
																{"uan",PY_mb_kuan}, 
																{"uang",PY_mb_kuang}, 
																{"ui",PY_mb_kui}, 
																{"un",PY_mb_kun}, 
																{"uo",PY_mb_kuo}};

MEM_ATTR struct PY_index		PY_index_l[]={{"a",PY_mb_la}, 
																{"ai",PY_mb_lai}, 
																{"an",PY_mb_lan}, 
																{"ang",PY_mb_lang}, 
																{"ao",PY_mb_lao}, 
																{"e",PY_mb_le}, 
																{"ei",PY_mb_lei}, 
																{"eng",PY_mb_leng}, 
																{"i",PY_mb_li}, 
																{"ia",PY_mb_lia},
																{"ian",PY_mb_lian}, 
																{"iang",PY_mb_liang}, 
																{"iao",PY_mb_liao}, 
																{"ie",PY_mb_lie}, 
																{"in",PY_mb_lin}, 
																{"ing",PY_mb_ling}, 
																{"iu",PY_mb_liu}, 
																{"o",PY_mb_lo},
																{"ong",PY_mb_long}, 
																{"ou",PY_mb_lou}, 
																{"u",PY_mb_lu}, 
																{"uan",PY_mb_luan}, 
																{"ue",PY_mb_lue}, 
																{"un",PY_mb_lun}, 
																{"uo",PY_mb_luo}, 
																{"v",PY_mb_lv}};

MEM_ATTR struct PY_index		PY_index_m[]={{"a",PY_mb_ma}, 
																{"ai",PY_mb_mai}, 
																{"an",PY_mb_man}, 
																{"ang",PY_mb_mang}, 
																{"ao",PY_mb_mao}, 
																{"e",PY_mb_me}, 
																{"ei",PY_mb_mei}, 
																{"en",PY_mb_men}, 
																{"eng",PY_mb_meng}, 
																{"i",PY_mb_mi}, 
																{"ian",PY_mb_mian}, 
																{"iao",PY_mb_miao}, 
																{"ie",PY_mb_mie}, 
																{"in",PY_mb_min}, 
																{"ing",PY_mb_ming}, 
																{"iu",PY_mb_miu}, 
																{"o",PY_mb_mo}, 
																{"ou",PY_mb_mou}, 
																{"u",PY_mb_mu}};

MEM_ATTR struct PY_index		PY_index_n[]={{"a",PY_mb_na}, 
																{"ai",PY_mb_nai}, 
																{"an",PY_mb_nan}, 
																{"ang",PY_mb_nang}, 
																{"ao",PY_mb_nao}, 
																{"e",PY_mb_ne}, 
																{"ei",PY_mb_nei}, 
																{"en",PY_mb_nen}, 
																{"eng",PY_mb_neng}, 
																{"g",PY_mb_ng},
																{"i",PY_mb_ni}, 
																{"ian",PY_mb_nian}, 
																{"iang",PY_mb_niang}, 
																{"iao",PY_mb_niao}, 
																{"ie",PY_mb_nie}, 
																{"in",PY_mb_nin}, 
																{"ing",PY_mb_ning}, 
																{"iu",PY_mb_niu}, 
																{"ong",PY_mb_nong}, 
																{"ou",PY_mb_nou},
																{"u",PY_mb_nu}, 
																{"uan",PY_mb_nuan}, 
																{"ue",PY_mb_nue}, 
																{"uo",PY_mb_nuo}, 
																{"v",PY_mb_nv}};

MEM_ATTR struct PY_index		PY_index_o[]={{"",PY_mb_o}, 
																{"u",PY_mb_ou}};

MEM_ATTR struct PY_index		PY_index_p[]={{"a",PY_mb_pa}, 
																{"ai",PY_mb_pai}, 
																{"an",PY_mb_pan}, 
																{"ang",PY_mb_pang}, 
																{"ao",PY_mb_pao}, 
																{"ei",PY_mb_pei}, 
																{"en",PY_mb_pen}, 
																{"eng",PY_mb_peng}, 
																{"i",PY_mb_pi}, 
																{"ian",PY_mb_pian}, 
																{"iao",PY_mb_piao}, 
																{"ie",PY_mb_pie}, 
																{"in",PY_mb_pin}, 
																{"ing",PY_mb_ping}, 
																{"o",PY_mb_po}, 
																{"ou",PY_mb_pou}, 
																{"u",PY_mb_pu}};

MEM_ATTR struct PY_index		PY_index_q[]={{"i",PY_mb_qi}, 
																{"ia",PY_mb_qia}, 
																{"ian",PY_mb_qian}, 
																{"iang",PY_mb_qiang}, 
																{"iao",PY_mb_qiao}, 
																{"ie",PY_mb_qie}, 
																{"in",PY_mb_qin}, 
																{"ing",PY_mb_qing}, 
																{"iong",PY_mb_qiong}, 
																{"iu",PY_mb_qiu}, 
																{"u",PY_mb_qu}, 
																{"uan",PY_mb_quan}, 
																{"ue",PY_mb_que}, 
																{"ui",PY_mb_qui},
																{"un",PY_mb_qun}};

MEM_ATTR struct PY_index		PY_index_r[]={{"an",PY_mb_ran}, 
																{"ang",PY_mb_rang}, 
																{"ao",PY_mb_rao}, 
																{"e",PY_mb_re}, 
																{"en",PY_mb_ren}, 
																{"eng",PY_mb_reng}, 
																{"i",PY_mb_ri}, 
																{"ong",PY_mb_rong}, 
																{"ou",PY_mb_rou}, 
																{"u",PY_mb_ru}, 
																{"uan",PY_mb_ruan}, 
																{"ui",PY_mb_rui}, 
																{"un",PY_mb_run}, 
																{"uo",PY_mb_ruo}};

MEM_ATTR struct PY_index		PY_index_s[]={{"a",PY_mb_sa}, 
																{"ai",PY_mb_sai}, 
																{"an",PY_mb_san}, 
																{"ang",PY_mb_sang}, 
																{"ao",PY_mb_sao}, 
																{"e",PY_mb_se}, 
																{"en",PY_mb_sen}, 
																{"eng",PY_mb_seng}, 
																{"ha",PY_mb_sha}, 
																{"hai",PY_mb_shai}, 
																{"han",PY_mb_shan}, 
																{"hang",PY_mb_shang}, 
																{"hao",PY_mb_shao}, 
																{"he",PY_mb_she}, 
																{"hei",PY_mb_shei},
																{"hen",PY_mb_shen}, 
																{"heng",PY_mb_sheng}, 
																{"hi",PY_mb_shi}, 
																{"hou",PY_mb_shou}, 
																{"hu",PY_mb_shu}, 
																{"hua",PY_mb_shua}, 
																{"huai",PY_mb_shuai}, 
																{"huan",PY_mb_shuan}, 
																{"huang",PY_mb_shuang}, 
																{"hui",PY_mb_shui}, 
																{"hun",PY_mb_shun}, 
																{"huo",PY_mb_shuo}, 
																{"i",PY_mb_si}, 
																{"ong",PY_mb_song}, 
																{"ou",PY_mb_sou}, 
																{"u",PY_mb_su}, 
																{"uan",PY_mb_suan}, 
																{"ui",PY_mb_sui}, 
																{"un",PY_mb_sun}, 
																{"uo",PY_mb_suo}};

MEM_ATTR struct PY_index		PY_index_t[]={{"a",PY_mb_ta}, 
																{"ai",PY_mb_tai}, 
																{"an",PY_mb_tan}, 
																{"ang",PY_mb_tang}, 
																{"ao",PY_mb_tao}, 
																{"e",PY_mb_te}, 
																{"eng",PY_mb_teng}, 
																{"i",PY_mb_ti}, 
																{"ian",PY_mb_tian}, 
																{"iao",PY_mb_tiao}, 
																{"ie",PY_mb_tie}, 
																{"ing",PY_mb_ting}, 
																{"ong",PY_mb_tong}, 
																{"ou",PY_mb_tou}, 
																{"u",PY_mb_tu}, 
																{"uan",PY_mb_tuan}, 
																{"ui",PY_mb_tui}, 
																{"un",PY_mb_tun}, 
																{"uo",PY_mb_tuo}};

MEM_ATTR struct PY_index		PY_index_u[]={{"",PY_mb_space}};

MEM_ATTR struct PY_index		PY_index_v[]={{"",PY_mb_space}};

MEM_ATTR struct PY_index		PY_index_w[]={{"a",PY_mb_wa}, 
																{"ai",PY_mb_wai}, 
																{"an",PY_mb_wan}, 
																{"ang",PY_mb_wang}, 
																{"ei",PY_mb_wei}, 
																{"en",PY_mb_wen}, 
																{"eng",PY_mb_weng}, 
																{"o",PY_mb_wo}, 
																{"u",PY_mb_wu}};

MEM_ATTR struct PY_index		PY_index_x[]={{"i",PY_mb_xi}, 
																{"ia",PY_mb_xia}, 
																{"ian",PY_mb_xian},
																{"iang",PY_mb_xiang}, 
																{"iao",PY_mb_xiao}, 
																{"ie",PY_mb_xie}, 
																{"in",PY_mb_xin}, 
																{"ing",PY_mb_xing}, 
																{"iong",PY_mb_xiong}, 
																{"iu",PY_mb_xiu}, 
																{"u",PY_mb_xu}, 
																{"uan",PY_mb_xuan}, 
																{"ue",PY_mb_xue}, 
																{"un",PY_mb_xun}};

MEM_ATTR struct PY_index		PY_index_y[]={{"a",PY_mb_ya}, 
																{"an",PY_mb_yan}, 
																{"ang",PY_mb_yang}, 
																{"ao",PY_mb_yao}, 
																{"e",PY_mb_ye}, 
																{"i",PY_mb_yi}, 
																{"in",PY_mb_yin}, 
																{"ing",PY_mb_ying}, 
																{"o",PY_mb_yo}, 
																{"ong",PY_mb_yong}, 
																{"ou",PY_mb_you}, 
																{"u",PY_mb_yu}, 
																{"uan",PY_mb_yuan}, 
																{"ue",PY_mb_yue}, 
																{"un",PY_mb_yun}};

MEM_ATTR struct PY_index		PY_index_z[]={{"a",PY_mb_za}, 
																{"ai",PY_mb_zai}, 
																{"an",PY_mb_zan}, 
																{"ang",PY_mb_zang}, 
																{"ao",PY_mb_zao}, 
																{"e",PY_mb_ze}, 
																{"ei",PY_mb_zei}, 
																{"en",PY_mb_zen}, 
																{"eng",PY_mb_zeng}, 
																{"ha",PY_mb_zha}, 
																{"hai",PY_mb_zhai}, 
																{"han",PY_mb_zhan}, 
																{"hang",PY_mb_zhang}, 
																{"hao",PY_mb_zhao}, 
																{"he",PY_mb_zhe}, 
																{"hen",PY_mb_zhen}, 
																{"heng",PY_mb_zheng}, 
																{"hi",PY_mb_zhi}, 
																{"hong",PY_mb_zhong}, 
																{"hou",PY_mb_zhou}, 
																{"hu",PY_mb_zhu}, 
																{"hua",PY_mb_zhua}, 
																{"huai",PY_mb_zhuai}, 
																{"huan",PY_mb_zhuan}, 
																{"huang",PY_mb_zhuang}, 
																{"hui",PY_mb_zhui}, 
																{"hun",PY_mb_zhun}, 
																{"huo",PY_mb_zhuo}, 
																{"i",PY_mb_zi}, 
																{"ong",PY_mb_zong}, 
																{"ou",PY_mb_zou}, 
																{"u",PY_mb_zu}, 
																{"uan",PY_mb_zuan}, 
																{"ui",PY_mb_zui}, 
																{"un",PY_mb_zun}, 
																{"uo",PY_mb_zuo}};

MEM_ATTR struct PY_index		PY_index_end[]={{"",PY_mb_space}}; 

MEM_ATTR unsigned index_number[] = {
		5, //a
		16,//b
		34,//c
		22,//d
		4, //e
		9, //f
		19,//g
		19,//h
		1, //i
		14,//j
		18,//k
		26,//l
		19,//m
		25,//n
		2, //o
		17,//p
		15,//q
		14,//r
		35,//s
		19,//t
		1, //u
		1, //v
		9, //w
		14,//x
		15,//y
		36,//z
};
/*定义首字母索引表*/ 
MEM_ATTR struct PY_index *PY_index_headletter[]={PY_index_a, 
																				PY_index_b, 
																				PY_index_c, 
																				PY_index_d, 
																				PY_index_e, 
																				PY_index_f, 
																				PY_index_g, 
																				PY_index_h, 
																				PY_index_i, 
																				PY_index_j, 
																				PY_index_k, 
																				PY_index_l, 
																				PY_index_m, 
																				PY_index_n, 
																				PY_index_o, 
																				PY_index_p, 
																				PY_index_q, 
																				PY_index_r, 
																				PY_index_s, 
																				PY_index_t, 
																				PY_index_u, 
																				PY_index_v, 
																				PY_index_w, 
																				PY_index_x, 
																				PY_index_y, 
																				PY_index_z, 
																				PY_index_end}; 

char * py_ime(char *strInput_py_str)																				/* 获取拼音串 */ 
{ 
		MEM_ATTR struct PY_index *cpHZ; 
        uint16_t i,cInputStrLength;
		cInputStrLength=strlen(strInput_py_str);																/*输入拼音串长度*/ 
		if(*strInput_py_str=='\0') 
		{
				return NULL;																												/*如果输入空字符返回0*/ 
		}
		
		for(i=0;i<cInputStrLength;i++) 
		{
				*(strInput_py_str+i)|=0x20;																					/*将字母串转为小写*/ 
		}
		
		if(*strInput_py_str=='i') 
		{
				return NULL;																												/*错误拼音输入*/ 
		}
		
		if(*strInput_py_str=='u') 
		{
				return NULL; 
		}
		
		if(*strInput_py_str=='v') 
		{
				return NULL; 
		}

		cpHZ = PY_index_headletter[strInput_py_str[0] - 'a'];										/*查首字母索引*/ 
		uint16_t count = index_number[strInput_py_str[0] - 'a'];
		//    cpHZedge = PY_index_headletter[strInput_py_str[0] - 'a' + 1];  		/*设置指针界限*/ 
		//    printf("%x, %x\n", cpHZ, cpHZedge);
		strInput_py_str++;																											/*指向拼音串第二个字母*/ 
		while(count--)																													/*索引表不超界*/ 
		{ 
				for(i=0;i<cInputStrLength;i++) 
				{ 
						if(*(strInput_py_str+i)!=*((*cpHZ).PY+i)) 
						{
								break;																											/*发现字母串不配,退出*/ 
						}
				} 
				if(i==cInputStrLength)			/*字母串全配*/ 
				{ 
						return (char *)(*cpHZ).PY_mb; 
				} 
				cpHZ++; 
		} 
        #if 22
        if (cInputStrLength == 1)
        {
            strInput_py_str--;
            cpHZ = PY_index_headletter[strInput_py_str[0] - 'a'];										/*查首字母索引*/ 
            return (char *)(*cpHZ).PY_mb; 
        }
        else
        {
            return NULL;	
        }
        
        #else
		return NULL;	

        #endif
} 
